const express=require('express')
const cors=require('cors')
const app=express()
// 数据库
const pool=require('./utils/db')
const Response=require('./utils/Response')
// 跨域
app.use( cors({
    origin:'*'
}))

// 解析 post 请求参数
app.use(express.json())
app.use(express.urlencoded({ extended: false }))

app.listen(3765,() => {
    console.log('start server 3765');
})
// 
app.use(express.static("./public"));

/** 
 * 用户接口
 */
// 用户ID查询
app.post('/user/id',(req,res) => {
    let user_id = req.body.user_id  //接收的参数格式，
    // console.log('用户ID查询接收参数：',req.body)
    // console.log(user_id)
    let sql = "select * from cyss_user where user_id=?"
    // console.log('object');
    pool.query(sql,[user_id],(error,result) => {
        if (error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})
//用户登录  --前台用户登录
app.post('/user/login',(req,res)=>{
    let {phone,upwd}=req.body
    // console.log('接收的用户信息',req.body);
    let sql="select * from cyss_user where user_phone=? and user_pwd=?"
    pool.query(sql,[phone,upwd],(error,result)=>{
        if(error){
            res.send(Response.error(500,error))
            throw error
        }
        if(result.length==0){
            res.send(Response.error(400,'账号密码输入错误'))
        }else{
            res.send(Response.ok(result))
        }
    })
})
// 新增用户
app.post('/user-add',(req,res)=>{
    let {user_phone,user_pwd,user_name,user_email}=req.body
    
    let sql="insert into cyss_user (user_phone,user_pwd,user_name,user_email) values(?,?,?,?)"
    
    pool.query(sql,[user_phone,user_pwd,user_name,user_email],(error,result)=>{
        if(error){
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok())
    })
    
})
/**
 * 景点接口
 */
//名称模糊查询景点  前台搜索页面
app.post('/scenic-name',(req,res)=>{
    let {name} =req.body
    // console.log('景点查询接收参数',req.body);
    let sql="select * from cyss_scenic where scenic_name like ?"
    pool.query(sql,[`%${name}%`],(error,result)=>{
        if(error){
            res.send(Response(500,error))
            throw error
        } 
        // else if (result.length === 0) {
        //     res.send()
        // }
        res.send(Response.ok(result))
    })
})
app.post('/scenic/id',(req,res)=>{
    let id =req.body.scenic_id
    // console.log('景点ID',req.body.scenic_id);
    let sql="select * from cyss_scenic where scenic_id=?"
    pool.query(sql,[id],(error,result)=>{
        if(error){
            res.send(Response(500,error))
            throw error
        } 
        // else if (result.length === 0) {
        //     res.send()
        // }
        res.send(Response.ok(result))
    })
})
/**
 * 评论接口
 */
// 发布评论
app.post('/disAdd',(req,res) => {
    let {content,userId,lineId,disTime } =req.body
    // debugger
    // console.log('用户评论接收：',req.body);
    let sql = "insert into cyss_discuss (dis_content , user_id, line_id,dis_time ) values(?,?,?,?)"
    pool.query(sql,[content,userId,lineId,disTime],(error,result) => {
        if(error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})

// 根据线路ID 查询评论
app.post('/disLineID',(req,res) => {
    
    let {lineId} = req.body
    // console.log(req.body);
    let sql = "select * from cyss_discuss where line_id=?"
    // console.log('评论列表查询',sql);
    pool.query(sql,[lineId],(error,result) => {
        if (error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})
/**
 * 订单接口
 */
//查询订单表  -- 用户id和订单id => SQL不会
app.post('/order/userId',(req,res)=>{
    let { userId } = req.body
    // console.log('订单用户ID查询：',req.body);
    let sql = "select * from cyss_order where user_id=?"
    // console.log('订单用户ID查询SQL：',sql);
    pool.query(sql,[userId],(error,result) => {
        if(error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})
//添加订单 --前台接口
app.post('/order-add',(req,res)=>{
    let { user_id, line_id,ordered_time,total_price,line_name, line_journey } = req.body
    // console.log('接收下单参数：',req.body);
    let sql = "insert into cyss_order (user_id, line_id,ordered_time,total_price,line_name, line_journey) values(?,?,?,?,?,?)"
    pool.query(sql,[user_id, line_id,ordered_time,total_price,line_name, line_journey],(error,result) => {
        if(error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})
//下单 -- 景点单独购买
app.post('/orderScenic',(req,res) => {
    let {user_id, scenic_id,total_price,ordered_time,scenic_name} =req.body
    // console.log('下单参数',req.body);
    let sql = "insert into cyss_order (user_id, scenic_id,total_price,ordered_time,scenic_name) values (?,?,?,?,?)"
    pool.query(sql,[user_id, scenic_id,total_price,ordered_time,scenic_name],(error,result) => {
        if (error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
}) 
// 根据订单ID删除订单
app.post('/order/del', (req,res) => {
    let { orderId } = req.body
    // console.log('删除订单', req.body);
    let sql = "delete from cyss_order where order_id = ?"
    pool.query(sql, [orderId], (error,result) => {
        if(error) {
            res.send(Response.error(500, error))
            throw error
        }
        res.send(Response.ok(result))
    })
})
/**
 * 线路接口
 */
app.get('/line-list',(req,res)=>{
    let {page,pagesize} = req.query
    // console.log('查询线路列表接收：',req.query);
    let startIndex= (page - 1) * 10
    let size= parseInt(pagesize)
    let sql ="select * from cyss_line limit ?,?"
    pool.query(sql,[startIndex,size],(error,result)=>{
        if(error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})
//按ID 查询线路信息，装填到页面中 
app.post('/lineId',(req,res) => {
    let {lineId} = req.body
    // console.log('线路ID查询：',{lineId})
    let sql = "select * from cyss_line where line_id=?"
    pool.query(sql,[lineId],(error,result) => {
        if (error) {
            res.send(Response.error(500,error))
            throw error
        }
        res.send(Response.ok(result))
    })
})